<!DOCTYPE html>
<html lang="zh-CN">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  <link rel="icon" href="/blog/images/logo.png">
  
  <title>【Java】常见的日志框架 | 溪岚花的博客</title>
  
  <meta name="author" content="林家隆" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="Java" />
  
  <meta name="description" content="JAVA 日志框架Java 日志框架主要关注以下问题：  日志级别。 日志输出格式。 日志输出位置。  JUL 日志框架介绍JUL 全称 Java util Logging，是 Java 原生的日志框架。 日志组件 Loggers：应用程序通过 Logger 对象来输出日志信息。 Handlers：每个 Logger 对象都会关联一个 Handler 对象，Logger 对象会将日志信息交给 Ha">
<meta property="og:type" content="article">
<meta property="og:title" content="【Java】常见的日志框架">
<meta property="og:url" content="https://lin316.gitee.io/blog/2024/04/07/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/index.html">
<meta property="og:site_name" content="溪岚花的博客">
<meta property="og:description" content="JAVA 日志框架Java 日志框架主要关注以下问题：  日志级别。 日志输出格式。 日志输出位置。  JUL 日志框架介绍JUL 全称 Java util Logging，是 Java 原生的日志框架。 日志组件 Loggers：应用程序通过 Logger 对象来输出日志信息。 Handlers：每个 Logger 对象都会关联一个 Handler 对象，Logger 对象会将日志信息交给 Ha">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://lin316.gitee.io/blog/blog/images/logo.png">
<meta property="article:published_time" content="2024-04-07T01:00:00.000Z">
<meta property="article:modified_time" content="2025-11-20T11:35:22.197Z">
<meta property="article:author" content="林家隆">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://lin316.gitee.io/blog/blog/images/logo.png">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/blog/css/kratosr.min.css" media="all"></script>
  
    <link rel="stylesheet" id="darkmode-css" href="/blog/css/kr-color-dark.min.css" media="(prefers-color-scheme: dark)"></script>
    <script src="/blog/js/kr-dark.min.js"></script>
  
  
    <link rel="stylesheet" id="highlight-css" href="/blog/css/highlight/night-eighties.min.css" media="all"></script>
  
  <link rel="stylesheet" id="fontawe-css" href="/blog/vendors/font-awesome@4.7.0/css/font-awesome.min.css" media="all"></script>
  <link rel="stylesheet" id="nprogress-css" href="/blog/vendors/nprogress@0.2.0/nprogress.css" media="all"></script>
  
  
  
    <link rel="stylesheet" href="/blog/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css"></script>
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="/blog/vendors/jquery@3.6.0/dist/jquery.min.js"></script>
  
  
  <style>
    
    
  </style>
  
<meta name="generator" content="Hexo 6.3.0"></head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <header id="kratos-desktop-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="nav-header">
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                        <li>
                                            
                                                <a href="/blog/">
                                            
                                                
                                                    <i class="fa fa-home"></i>
                                                
                                                首页
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/archives/">
                                            
                                                
                                                    <i class="fa fa-file"></i>
                                                
                                                归档
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/tags/">
                                            
                                                
                                                    <i class="fa fa-tag"></i>
                                                
                                                标签
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/categories/">
                                            
                                                
                                                    <i class="fa fa-folder"></i>
                                                
                                                目录
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/search/">
                                            
                                                
                                                    <i class="fa fa-search"></i>
                                                
                                                搜索
                                            </a>
                                            
                                        </li>
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <header id="kratos-mobile-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="color-logo"><a href="/blog/">溪岚花的博客</a></div>
                    <div class="nav-toggle">
                        <a class="kratos-nav-toggle js-kratos-nav-toggle">
                            <i></i>
                        </a>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/blog/">
                        <h2 style="opacity: 0.2;">溪岚花的博客</h2> <br />
                        <span></span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div id="main" class="row">
                    

        

            <section class="col-md-8">

        

            <article itemscope itemtype="https://schema.org/Article">
    
    <link itemprop="mainEntityOfPage" href="https://lin316.gitee.io/blog/2024/04/07/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/">
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            
                <h1 class="kratos-entry-title text-center" itemprop="name headline">【Java】常见的日志框架</h1>
            
            
            <ul class="kratos-post-meta text-center">
                <li><time datetime="2024-04-07T01:00:00.000Z" itemprop="datePublished"><i class="fa fa-calendar"></i> 2024-04-07</time></li>
                <li itemprop="author" itemscope itemtype="https://schema.org/Person">
                    <i class="fa fa-user"></i> 作者 <span itemprop="name">林家隆</span>
                </li>
                
                    <li>
                        <i class="fa fa-edit"></i> 
                        
                        
                            21.00K
                        
                        字
                    </li>
                
                
            </ul>
        </header>
        <div class="kratos-post-content">
            
            <div id="expire-alert" class="alert alert-warning hidden" role="alert">
                <div class="icon"><i class="fa fa-warning"></i></div>
                <div class="text"><p>本文最后编辑于 <time datetime="1763638522197"></time> 前，其中的内容可能需要更新。</p></div>
            </div>
            
            
            
                <div class="kratos-post-inner-toc toc-div-class" >
                    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JAVA-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-number">1.</span> <span class="toc-text">JAVA 日志框架</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JUL-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-number">2.</span> <span class="toc-text">JUL 日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D"><span class="toc-number">2.1.</span> <span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E5%BF%97%E7%BB%84%E4%BB%B6"><span class="toc-number">2.2.</span> <span class="toc-text">日志组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B"><span class="toc-number">2.3.</span> <span class="toc-text">使用示例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E6%97%A5%E5%BF%97%E9%85%8D%E7%BD%AE"><span class="toc-number">2.4.</span> <span class="toc-text">修改日志配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E4%BB%A3%E7%A0%81%E4%BF%AE%E6%94%B9"><span class="toc-number">2.4.1.</span> <span class="toc-text">通过代码修改</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9"><span class="toc-number">2.4.2.</span> <span class="toc-text">通过配置文件修改</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Log4j-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-number">3.</span> <span class="toc-text">Log4j 日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E5%BF%97%E7%BB%84%E4%BB%B6-1"><span class="toc-number">3.1.</span> <span class="toc-text">日志组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">3.2.</span> <span class="toc-text">配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-1"><span class="toc-number">3.3.</span> <span class="toc-text">使用示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JCL-%E6%97%A5%E5%BF%97%E6%8E%A5%E5%8F%A3"><span class="toc-number">4.</span> <span class="toc-text">JCL 日志接口</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D-1"><span class="toc-number">4.1.</span> <span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-2"><span class="toc-number">4.2.</span> <span class="toc-text">使用示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SLF4J-%E6%97%A5%E5%BF%97%E6%8E%A5%E5%8F%A3"><span class="toc-number">5.</span> <span class="toc-text">SLF4J 日志接口</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D-2"><span class="toc-number">5.1.</span> <span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SLF4J-%E6%95%B4%E5%90%88%E5%85%B6%E5%AE%83%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-number">5.2.</span> <span class="toc-text">SLF4J 整合其它日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Log4j2"><span class="toc-number">5.2.1.</span> <span class="toc-text">SLF4J + Log4j2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Logback"><span class="toc-number">5.2.2.</span> <span class="toc-text">SLF4J + Logback</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-NOP"><span class="toc-number">5.2.3.</span> <span class="toc-text">SLF4J + NOP</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-JCL"><span class="toc-number">5.2.4.</span> <span class="toc-text">SLF4J + JCL</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Log4j"><span class="toc-number">5.2.5.</span> <span class="toc-text">SLF4J + Log4j</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-JUL"><span class="toc-number">5.2.6.</span> <span class="toc-text">SLF4J + JUL</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Slf4j-simple"><span class="toc-number">5.2.7.</span> <span class="toc-text">SLF4J + Slf4j-simple</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A1%A5%E6%8E%A5%E6%97%A7%E7%9A%84%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-number">5.3.</span> <span class="toc-text">桥接旧的日志框架</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Logback-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-number">6.</span> <span class="toc-text">Logback 日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E5%BF%97%E7%BB%84%E4%BB%B6-2"><span class="toc-number">6.1.</span> <span class="toc-text">日志组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-1"><span class="toc-number">6.2.</span> <span class="toc-text">配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-3"><span class="toc-number">6.3.</span> <span class="toc-text">使用示例</span></a></li></ol></li></ol>
                </div>
            
            <hr />
            <div itemprop="articleBody"><h2 id="JAVA-日志框架"><a href="#JAVA-日志框架" class="headerlink" title="JAVA 日志框架"></a>JAVA 日志框架</h2><p>Java 日志框架主要关注以下问题：</p>
<ul>
<li>日志级别。</li>
<li>日志输出格式。</li>
<li>日志输出位置。</li>
</ul>
<h2 id="JUL-日志框架"><a href="#JUL-日志框架" class="headerlink" title="JUL 日志框架"></a>JUL 日志框架</h2><h3 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h3><p>JUL 全称 Java util Logging，是 Java 原生的日志框架。</p>
<h3 id="日志组件"><a href="#日志组件" class="headerlink" title="日志组件"></a>日志组件</h3><ul>
<li><strong>Loggers：</strong>应用程序通过 Logger 对象来输出日志信息。</li>
<li><strong>Handlers：</strong>每个 Logger 对象都会关联一个 Handler 对象，Logger 对象会将日志信息交给 Handlers 对象处理，由 Handlers 对象决定日志的输出位置。</li>
<li><strong>Formatter：</strong>每个 Handler 对象都会关联一个 Formatter 对象，由 Formatter 对象对日志信息格式化。</li>
<li><strong>Filters：</strong>过滤器，例如对日志级别进行过滤。</li>
</ul>
<h3 id="使用示例"><a href="#使用示例" class="headerlink" title="使用示例"></a>使用示例</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JulTest</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testJul</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> Logger.getLogger(<span class="string">&quot;blog.lin.JulTest&quot;</span>);</span><br><span class="line">        logger.severe(<span class="string">&quot;severe&quot;</span>);</span><br><span class="line">        logger.warning(<span class="string">&quot;warning&quot;</span>);</span><br><span class="line">        logger.info(<span class="string">&quot;info&quot;</span>);</span><br><span class="line">        logger.config(<span class="string">&quot;cofnig&quot;</span>);</span><br><span class="line">        logger.fine(<span class="string">&quot;fine&quot;</span>);</span><br><span class="line">        logger.finer(<span class="string">&quot;finer&quot;</span>);</span><br><span class="line">        logger.finest(<span class="string">&quot;finest&quot;</span>);</span><br><span class="line">		<span class="comment">// 通过占位符传参数</span></span><br><span class="line">        logger.log(Level.INFO, <span class="string">&quot;用户信息：&#123;0&#125;,&#123;1&#125;&quot;</span>, <span class="keyword">new</span> <span class="title class_">Object</span>[]&#123;<span class="string">&quot;lin&quot;</span>, <span class="number">18</span>&#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>JUL 中的日志级别</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">Level.SEVERE（最高值）</span><br><span class="line">Level.WARNING</span><br><span class="line">Level.INFO （默认级别）</span><br><span class="line">Level.CONFIG</span><br><span class="line">Level.FINE</span><br><span class="line">Level.FINER</span><br><span class="line">Level.FINEST（最低值）</span><br><span class="line"></span><br><span class="line">Level.OFF（表示不输出任何日志）</span><br><span class="line">Level.ALL（表示输出所有级别的日志）</span><br></pre></td></tr></table></figure>
</blockquote>
<h3 id="修改日志配置"><a href="#修改日志配置" class="headerlink" title="修改日志配置"></a>修改日志配置</h3><blockquote>
<p><strong>Logger 之间的父子关系</strong></p>
<p>JUL 中的 Logger 对象之间是存在父子关系的。例如 name 为 blog.lin.JulTest 的 Logger 对象是 name 为  blog.lin 的 Logger 对象的 父 Logger。</p>
<p>所有 Logger 对象的顶级父 Logger 是 RootLogger 对象。</p>
<p>如果 Logger 对象的 useParentHandlers 属性为 true，表示该 Logger 对象会使用父 Logger 的配置，如果父 Logger 的 useParentHandlers  属性也为 true，则会一层一层往上找，直到找到 RootLogger 对象的配置。</p>
</blockquote>
<h4 id="通过代码修改"><a href="#通过代码修改" class="headerlink" title="通过代码修改"></a>通过代码修改</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JulTest</span> &#123;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testJul</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> Logger.getLogger(<span class="string">&quot;blog.lin.JulTest&quot;</span>);</span><br><span class="line">        <span class="comment">// 是否使用父 logger 的配置。false 表示不使用。</span></span><br><span class="line">        logger.setUseParentHandlers(<span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 创建 handler 对象，配置日志信息的输出位置。consoleHandler 对象会将日志信息输出到 console 控制台</span></span><br><span class="line">        <span class="type">ConsoleHandler</span> <span class="variable">consoleHandler</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ConsoleHandler</span>();</span><br><span class="line">        <span class="comment">// 为 handler 对象配置 formatter 对象，配置信息的输出格式</span></span><br><span class="line">        consoleHandler.setFormatter(<span class="keyword">new</span> <span class="title class_">SimpleFormatter</span>());</span><br><span class="line">        <span class="comment">// 配置日志的输出级别。handler 对象和 logger 对象都需要配置，否则最小配置的日志级别输出</span></span><br><span class="line">        consoleHandler.setLevel(Level.ALL);</span><br><span class="line">        logger.setLevel(Level.ALL);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 为 logger 添加配置</span></span><br><span class="line">        logger.addHandler(consoleHandler);</span><br><span class="line"></span><br><span class="line">        logger.info(<span class="string">&quot;info&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="通过配置文件修改"><a href="#通过配置文件修改" class="headerlink" title="通过配置文件修改"></a>通过配置文件修改</h4><p>在 resources 目录下编写  logging.properties 配置文件：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## 配置 RootLogger 对象的属性配置</span></span><br><span class="line"><span class="comment"># RootLogger 对象的处理器</span></span><br><span class="line"><span class="attr">handlers</span>=<span class="string">java.util.logging.ConsoleHandler</span></span><br><span class="line"><span class="comment"># RootLogger 对象的日志等级</span></span><br><span class="line"><span class="attr">.level</span>=<span class="string">INFO</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## 配置 consoleHandler 对象的属性</span></span><br><span class="line"><span class="comment"># 日志的输出级别</span></span><br><span class="line"><span class="attr">java.util.logging.ConsoleHandler.level</span>=<span class="string">INFO</span></span><br><span class="line"><span class="comment"># 日志的输出格式</span></span><br><span class="line"><span class="attr">java.util.logging.ConsoleHandler.formatter</span>=<span class="string">java.util.logging.SimpleFormatter</span></span><br><span class="line"><span class="comment"># 日志的字符编码</span></span><br><span class="line"><span class="attr">java.util.logging.ConsoleHandler.encoding</span>=<span class="string">UTF-8</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## 配置 fileHandler 对象的属性</span></span><br><span class="line"><span class="comment"># 日志的输出级别</span></span><br><span class="line"><span class="attr">java.util.logging.FileHandler.level</span>=<span class="string">INFO</span></span><br><span class="line"><span class="comment"># 日志的输出格式</span></span><br><span class="line"><span class="attr">java.util.logging.FileHandler.formatter</span>=<span class="string">java.util.logging.SimpleFormatter</span></span><br><span class="line"><span class="comment"># 存储日志文件的路径</span></span><br><span class="line"><span class="attr">java.util.logging.FileHandler.pattern</span>=<span class="string">D:\\Temp\\jul.log</span></span><br><span class="line"><span class="comment"># 日志文件的大小限制，单位字节</span></span><br><span class="line"><span class="attr">java.util.logging.FileHandler.limit</span>=<span class="string">50000</span></span><br><span class="line"><span class="comment"># 日志文件的个数限制</span></span><br><span class="line"><span class="attr">java.util.logging.FileHandler.count</span>=<span class="string">1</span></span><br><span class="line"><span class="comment"># 为 true 表示在日志文件末尾追加日志，false 表示每次添加日志信息都会进行覆盖操作</span></span><br><span class="line"><span class="attr">java.util.logging.FileHandler.append</span>=<span class="string">true</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## 配置 simpleFormatter 对象的属性</span></span><br><span class="line"><span class="comment"># 指定日志消息的输出格式</span></span><br><span class="line"><span class="attr">java.util.logging.SimpleFormatter.format</span>=<span class="string">g4$s:%5$s[%1$tc]%n</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## 自定义 Logger 对象的属性配置</span></span><br><span class="line"><span class="comment"># 自定义 Logger 对象的处理器</span></span><br><span class="line"><span class="attr">blog.lin.JulTest.handlers</span>=<span class="string">java.util.logging.FileHandler</span></span><br><span class="line"><span class="comment"># 自定义 Logger 对象的日志级别</span></span><br><span class="line"><span class="attr">blog.lin.JulTest.level</span>=<span class="string">INFO</span></span><br><span class="line"><span class="comment"># 忽略父日志的配置</span></span><br><span class="line"><span class="attr">blog.lin.JulTest.useParentHandlers</span>=<span class="string">false</span></span><br></pre></td></tr></table></figure>

<p>读取自己编写的 logging.properties 配置：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JulTest</span> &#123;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testJul01</span><span class="params">()</span> <span class="keyword">throws</span> IOException &#123;</span><br><span class="line">        <span class="comment">// 读取 logging.properties 配置文件</span></span><br><span class="line">        <span class="type">InputStream</span> <span class="variable">in</span> <span class="operator">=</span> JulTest.class.getClassLoader().getResourceAsStream(<span class="string">&quot;logging.properties&quot;</span>);</span><br><span class="line">        <span class="comment">// 获取日志管理器对象</span></span><br><span class="line">        <span class="type">LogManager</span> <span class="variable">logManager</span> <span class="operator">=</span> LogManager.getLogManager();</span><br><span class="line">        <span class="comment">// 通过日志管理器对象加载配置文件</span></span><br><span class="line">        logManager.readConfiguration(in);</span><br><span class="line"></span><br><span class="line">        <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> Logger.getLogger(<span class="string">&quot;blog.lin.JulTest&quot;</span>);</span><br><span class="line">        logger.severe(<span class="string">&quot;severe&quot;</span>);</span><br><span class="line">        logger.warning(<span class="string">&quot;warning&quot;</span>);</span><br><span class="line">        logger.info(<span class="string">&quot;info&quot;</span>);</span><br><span class="line">        logger.config(<span class="string">&quot;cofnig&quot;</span>);</span><br><span class="line">        logger.fine(<span class="string">&quot;fine&quot;</span>);</span><br><span class="line">        logger.finer(<span class="string">&quot;finer&quot;</span>);</span><br><span class="line">        logger.finest(<span class="string">&quot;finest&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Log4j-日志框架"><a href="#Log4j-日志框架" class="headerlink" title="Log4j 日志框架"></a>Log4j 日志框架</h2><h3 id="日志组件-1"><a href="#日志组件-1" class="headerlink" title="日志组件"></a>日志组件</h3><ul>
<li><strong>Loggers：</strong>应用程序通过 Logger 对象来输出日志信息。</li>
<li><strong>Appenders：</strong>每个 Logger 对象可以关联多个 Appenders 对象，Logger 对象会将日志信息交给 Appenders 对象处理，由 Appenders 对象决定日志的输出位置。</li>
<li><strong>Layout：</strong>每个 Appenders 对象都会关联一个 Layout 对象，由 Layout  对象对日志信息格式化。</li>
</ul>
<blockquote>
<p><strong>Appenders 组件可选类型</strong></p>
<ul>
<li><p>ConsoleAppender：将日志输出到控制台。</p>
</li>
<li><p>FileAppender：将日志输出到文件中。</p>
</li>
<li><p>DailyRollingFileAppender：将日志输出到一个日志文件，文件名跟日期相关。</p>
</li>
<li><p>RollingFileAppender：将日志信息输出到一个日志文件，并且指定文件的尺寸，当文件大</p>
<p> 小达到指定尺寸时，会自动把文件改名，同时产生一个新的文件。如果文件数量达到最大值，就覆盖最旧的日志。</p>
</li>
<li><p>JDBCAppender：把日志信息保存到数据库中。</p>
</li>
</ul>
</blockquote>
<blockquote>
<p><strong>Layouts 组件可选类型</strong></p>
<ul>
<li>HTMLLayout：以 HTML 表示形式输出日志。</li>
<li>SimpleLayout：简单的日志格式，格式为（info - message）。</li>
<li>PatternLayout：自定义日志格式。</li>
</ul>
<blockquote>
<p><strong>自定义日志格式的占位符含义：</strong></p>
<ul>
<li><strong>%m：</strong>日志信息。</li>
<li><strong>%p：</strong>日志级别。</li>
<li><strong>%n：</strong>换行符。</li>
<li>%r：输出自应用启动到输出该 log 信息耗费的毫秒数</li>
<li><strong>%c：</strong>全限定类名。</li>
<li><strong>%t：</strong>线程名称。</li>
<li><strong>%d：</strong>服务器时间，也可以指定格式，如：%d{yyyy-MM-dd HH:mm:ss}</li>
<li>%F：文件名称。</li>
<li>%L：代码中的行号。</li>
<li>%l：输出日志时间发生的位置，包括类名、线程、及在代码中的行数。如：Test.main(Test.java:10)</li>
<li>%%：输出一个 “%” 字符</li>
</ul>
<p><strong>文本的对其方式：</strong></p>
<ul>
<li><strong>%5c：</strong>字符最少数量。如果字符数量小于 5，文本右对齐，左边用空格填充。</li>
<li><strong>%-5c：</strong>字符最少数量。如果字符数量小于 5，文本左对齐，右边用空格填充。</li>
<li><strong>%.5c：</strong>字符最大数量。如果字符数量大于5，会将左边多余的字符截断。</li>
</ul>
</blockquote>
</blockquote>
<h3 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h3><p>在 resources 目录下创建 log4j.properties 配置文件。</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## rootLogger 配置</span></span><br><span class="line"><span class="comment"># 指定 rootLogger 的日志级别和 appender 组件，appender 组件可以有多个</span></span><br><span class="line"><span class="attr">log4j.rootLogger</span>=<span class="string">INFO,console,file,rollingFile,dailyFile</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## 自定义 Logger 对象 配置</span></span><br><span class="line"><span class="attr">log4j.logger.blog.lin</span> = <span class="string">INFO,file</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## ConsoleAppender 配置</span></span><br><span class="line"><span class="attr">log4j.appender.console</span>=<span class="string">org.apache.log4j.ConsoleAppender</span></span><br><span class="line"><span class="comment"># 自定义日志的输出格式</span></span><br><span class="line"><span class="attr">log4j.appender.console.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="attr">log4j.appender.console.layout.conversionPattern</span>=<span class="string">%r [%t] %p %c %x - %m%n</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## FileAppender 配置</span></span><br><span class="line"><span class="attr">log4j.appender.file</span>=<span class="string">org.apache.log4j.FileAppender</span></span><br><span class="line"><span class="comment"># 自定义日志的输出格式</span></span><br><span class="line"><span class="attr">log4j.appender.file.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="attr">log4j.appender.file.layout.conversionPattern</span>=<span class="string">%r [%t] %p %c %x - %m%n</span></span><br><span class="line"><span class="comment"># 日志文件的生成路径</span></span><br><span class="line"><span class="attr">log4j.appender.file.file</span>=<span class="string">D:\\Temp\\log4j-file.log</span></span><br><span class="line"><span class="comment"># 日志文件的字符编码</span></span><br><span class="line"><span class="attr">log4j.appender.file.encoding</span>=<span class="string">UTF-8</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## RollingFileAppender 配置</span></span><br><span class="line"><span class="attr">log4j.appender.rollingFile</span>=<span class="string">org.apache.log4j.RollingFileAppender</span></span><br><span class="line"><span class="comment"># 自定义日志的输出格式</span></span><br><span class="line"><span class="attr">log4j.appender.rollingFile.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="attr">log4j.appender.rollingFile.layout.conversionPattern</span>=<span class="string">%r [%t] %p %c %x - %m%n</span></span><br><span class="line"><span class="comment"># 日志文件的生成路径</span></span><br><span class="line"><span class="attr">log4j.appender.rollingFile.file</span>=<span class="string">D:\\Temp\\log4j-rollingFile.log</span></span><br><span class="line"><span class="comment"># 日志文件的容量的最大值</span></span><br><span class="line"><span class="attr">log4j.appender.rollingFile.maxFileSize</span>=<span class="string">1MB</span></span><br><span class="line"><span class="comment"># 日志文件的最大数量</span></span><br><span class="line"><span class="attr">log4j.appender.rollingFile.maxBackupIndex</span>=<span class="string">10</span></span><br><span class="line"><span class="comment"># 日志文件的字符编码</span></span><br><span class="line"><span class="attr">log4j.appender.rollingFile.encoding</span>=<span class="string">UTF-8</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## DailyRollingFileAppender 配置</span></span><br><span class="line"><span class="attr">log4j.appender.dailyFile</span>=<span class="string">org.apache.log4j.DailyRollingFileAppender</span></span><br><span class="line"><span class="comment"># 自定义日志的输出格式</span></span><br><span class="line"><span class="attr">log4j.appender.dailyFile.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="attr">Log4j.appender.dailyFile.layout.conversionPattern</span>=<span class="string">%r [%t] %p %c %x - %m%n</span></span><br><span class="line"><span class="comment"># 日志文件的生成路径</span></span><br><span class="line"><span class="attr">log4j.appender.dailyFile.file</span>=<span class="string">D:\\Temp\\log4j-dailyFile.log</span></span><br><span class="line"><span class="comment"># 文件日期的拆分规则</span></span><br><span class="line"><span class="attr">log4j.appender.dailyFile.datePattern</span>=<span class="string">&#x27;.&#x27;yyyy-MM-dd-HH-mm-ss</span></span><br><span class="line"><span class="comment"># 日志文件的字符编码</span></span><br><span class="line"><span class="attr">Log4j.appender.dailyFile.encoding</span>=<span class="string">UTF-8</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## JDBCAppender 配置</span></span><br><span class="line"><span class="attr">log4j.appender.logDB</span>=<span class="string">org.apache.log4j.jdbc.JDBCAppender</span></span><br><span class="line"><span class="attr">log4j.appender.logDB.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="attr">log4j.appender.logDB.Driver</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="attr">log4j.appender.logDB.URL</span>=<span class="string">jdbc:mysql://&#123;数据库 ip&#125;:&#123;数据库 port&#125;/test</span></span><br><span class="line"><span class="attr">log4j.appender.logDB.User</span>=<span class="string">root</span></span><br><span class="line"><span class="attr">log4j.appender.logDB.Password</span>=<span class="string">123456</span></span><br><span class="line"><span class="attr">log4j.appender.logDB.Sql</span>=<span class="string">INSERT INTO log(project_name,create_date,level,category,file_name,thread_name,line,all_categ ory,message) values(&#x27;itcast&#x27;,&#x27;%d&#123;yyyy-MM-dd HH:mm:ss&#125;&#x27;,&#x27;%p&#x27;,&#x27;%c&#x27;,&#x27;%F&#x27;,&#x27;%t&#x27;,&#x27;%L&#x27;,&#x27;%l&#x27;,&#x27;%m&#x27;)</span></span><br></pre></td></tr></table></figure>

<p><strong>注：</strong></p>
<ul>
<li><p>如果自定义的 logger 和 父 logger 都配置了日志级别，以自定义的 logger 配置的日志级别为准。</p>
</li>
<li><p>如果自定义的 logger 和 父 logger 都配置了 appender 组件，那么所有配置的组件都会使用。如果配置了相同的组件，那么该组件就会使用两次。</p>
</li>
</ul>
<h3 id="使用示例-1"><a href="#使用示例-1" class="headerlink" title="使用示例"></a>使用示例</h3><p>导入 Log4j 依赖包。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.17<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>前提在 resources 目录下创建了 log4j.properties 配置文件。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.log4j.Logger;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Log4jTest</span> &#123;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testLog4j</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> Logger.getLogger(Log4jTest.class);</span><br><span class="line">        logger.fatal(<span class="string">&quot;fatal&quot;</span>);</span><br><span class="line">        logger.error(<span class="string">&quot;error&quot;</span>);</span><br><span class="line">        logger.warn(<span class="string">&quot;warn&quot;</span>);</span><br><span class="line">        logger.info(<span class="string">&quot;info&quot;</span>);</span><br><span class="line">        logger.debug(<span class="string">&quot;debug&quot;</span>);</span><br><span class="line">        logger.trace(<span class="string">&quot;trace&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>Log4j 的日志级别</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Level.FATAL（最高值）</span><br><span class="line">Level.ERROR</span><br><span class="line">Level.WARN</span><br><span class="line">Level.INFO</span><br><span class="line">Level.DEBUG（默认级别）</span><br><span class="line">Level.TRACE（最低值）</span><br><span class="line"></span><br><span class="line">Level.OFF（表示不输出任何日志）</span><br><span class="line">Level.ALL（表示输出所有级别的日志）</span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="JCL-日志接口"><a href="#JCL-日志接口" class="headerlink" title="JCL 日志接口"></a>JCL 日志接口</h2><h3 id="介绍-1"><a href="#介绍-1" class="headerlink" title="介绍"></a>介绍</h3><p>JCL 全称 Jakarta Commons Logging，是 Apache 提供的一个通用日志 API。</p>
<p>实现 JCL 接口的三个日志框架：log4j、jul、simpleLog。</p>
<p>如果一个项目导入多个 JCL 多个日志实现类，优先级为：Log4JLogger &gt; Jdk14Logger &gt; Jdk13LumberjackLogger &gt; SimpleLog。</p>
<h3 id="使用示例-2"><a href="#使用示例-2" class="headerlink" title="使用示例"></a>使用示例</h3><blockquote>
<p><strong>JCL + JUL 使用示例</strong></p>
<p>导入依赖包，不需要导入 JUL 依赖包，因为是 JDK 自带的日志框架：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>测试 JCL 接口 API：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.commons.logging.Log;</span><br><span class="line"><span class="keyword">import</span> org.apache.commons.logging.LogFactory;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JclTest</span> &#123;</span><br><span class="line">       <span class="meta">@Test</span></span><br><span class="line">       <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testJclOfJul</span><span class="params">()</span> &#123;</span><br><span class="line">           <span class="type">Log</span> <span class="variable">log</span> <span class="operator">=</span> LogFactory.getLog(JclTest.class);</span><br><span class="line">           log.info(<span class="string">&quot;info&quot;</span>);</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p><strong>JCL + Log4j 使用示例</strong></p>
<p>导入依赖包：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>commons-logging<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.17<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>编写 logging.properties 配置文件：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">## rootLogger 配置</span></span><br><span class="line"><span class="comment"># 指定 rootLogger 的日志级别和 appender 组件，appender 组件可以有多个</span></span><br><span class="line"><span class="attr">log4j.rootLogger</span>=<span class="string">INFO,file</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## 自定义 Logger 对象 配置</span></span><br><span class="line"><span class="attr">log4j.logger.blog.lin</span>=<span class="string">DEBUG,console</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## ConsoleAppender 配置</span></span><br><span class="line"><span class="attr">log4j.appender.console</span>=<span class="string">org.apache.log4j.ConsoleAppender</span></span><br><span class="line"><span class="comment"># 自定义日志的输出格式</span></span><br><span class="line"><span class="attr">log4j.appender.console.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="attr">log4j.appender.console.layout.conversionPattern</span>=<span class="string">%r [%t] %p %c %x - %m%n</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">## FileAppender 配置</span></span><br><span class="line"><span class="attr">log4j.appender.file</span>=<span class="string">org.apache.log4j.FileAppender</span></span><br><span class="line"><span class="comment"># 自定义日志的输出格式</span></span><br><span class="line"><span class="attr">log4j.appender.file.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="attr">log4j.appender.file.layout.conversionPattern</span>=<span class="string">%r [%t] %p %c %x - %m%n</span></span><br><span class="line"><span class="comment"># 日志文件的生成路径</span></span><br><span class="line"><span class="attr">log4j.appender.file.file</span>=<span class="string">D:\\Temp\\log4j-file.log</span></span><br><span class="line"><span class="comment"># 日志文件的字符编码</span></span><br><span class="line"><span class="attr">log4j.appender.file.encoding</span>=<span class="string">UTF-8</span></span><br></pre></td></tr></table></figure>

<p>测试 JCL 接口 API：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.commons.logging.Log;</span><br><span class="line"><span class="keyword">import</span> org.apache.commons.logging.LogFactory;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JclTest</span> &#123;</span><br><span class="line">       <span class="meta">@Test</span></span><br><span class="line">       <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testJclOfLog4j</span><span class="params">()</span> &#123;</span><br><span class="line">           <span class="type">Log</span> <span class="variable">log</span> <span class="operator">=</span> LogFactory.getLog(JclTest.class);</span><br><span class="line">           log.info(<span class="string">&quot;info&quot;</span>);</span><br><span class="line">           log.debug(<span class="string">&quot;debug&quot;</span>);</span><br><span class="line">           log.trace(<span class="string">&quot;trace&quot;</span>);</span><br><span class="line">       &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<h2 id="SLF4J-日志接口"><a href="#SLF4J-日志接口" class="headerlink" title="SLF4J 日志接口"></a>SLF4J 日志接口</h2><h3 id="介绍-2"><a href="#介绍-2" class="headerlink" title="介绍"></a>介绍</h3><p>SLF4J 作为通用的日志接口，整合了市面上常用的日志框架。例如：Log4j、JUC、JCL、Logback、Log4j2 等。</p>
<h3 id="SLF4J-整合其它日志框架"><a href="#SLF4J-整合其它日志框架" class="headerlink" title="SLF4J 整合其它日志框架"></a>SLF4J 整合其它日志框架</h3><p>使用 SLF4J 流程：</p>
<ul>
<li>如果日志框架已经实现了 SLF4J 的接口，直接添加对应日志依赖即可。</li>
<li>如果日志框架没有实现 SLF4J 的接口，需要先添加日志对 SLF4J 接口的适配器，然后再添加对应的日志依赖。</li>
<li>SLF4J 只能绑定一个日志实现框架。如果有多个日志实现框架，默认使用第一个依赖日志实现。</li>
</ul>
<h4 id="SLF4J-Log4j2"><a href="#SLF4J-Log4j2" class="headerlink" title="SLF4J + Log4j2"></a>SLF4J + Log4j2</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- Log4j2 API 接口 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.logging.log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.11.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- Log4j2 日志实现 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.logging.log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.11.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- 使用 slf4j 作为日志的门面 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.25<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- slf4j 绑定日志实现 log4j2 的适配器 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.logging.log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j-slf4j-impl<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.10.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="SLF4J-Logback"><a href="#SLF4J-Logback" class="headerlink" title="SLF4J + Logback"></a>SLF4J + Logback</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.25<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>ch.qos.logback<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>logback-classic<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="SLF4J-NOP"><a href="#SLF4J-NOP" class="headerlink" title="SLF4J + NOP"></a>SLF4J + NOP</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-nop<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>注：</strong>SLF4J 整合 NOP 日志框架表示停止输出日志。</p>
<h4 id="SLF4J-JCL"><a href="#SLF4J-JCL" class="headerlink" title="SLF4J + JCL"></a>SLF4J + JCL</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-jcl<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="SLF4J-Log4j"><a href="#SLF4J-Log4j" class="headerlink" title="SLF4J + Log4j"></a>SLF4J + Log4j</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.17<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="SLF4J-JUL"><a href="#SLF4J-JUL" class="headerlink" title="SLF4J + JUL"></a>SLF4J + JUL</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-jdk14<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="SLF4J-Slf4j-simple"><a href="#SLF4J-Slf4j-simple" class="headerlink" title="SLF4J + Slf4j-simple"></a>SLF4J + Slf4j-simple</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-simple<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="桥接旧的日志框架"><a href="#桥接旧的日志框架" class="headerlink" title="桥接旧的日志框架"></a>桥接旧的日志框架</h3><p>桥接的步骤：</p>
<ol>
<li>除去需要被替换的日志框架的依赖。</li>
<li>添加 SLF4J 提供的桥接框架。</li>
<li>为项目添加 SLF4J 日志实现框架。</li>
</ol>
<blockquote>
<p>SLF4J 提供的桥接框架：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- jcl --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jcl-over-slf4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- log4j --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j-over-slf4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- jul --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jul-to-slf4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">       <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.27<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>以下情况会抛出 StackOverflow Error 异常 ：</strong></p>
<ul>
<li>jcl-over-slf4j 依赖和 slf4j-jcl 依赖一起导入项目中。</li>
<li>log4j-over-slf4j 依赖和 slf4j-log4j12 依赖一起导入项目中。</li>
<li>jul-to-slf4j.jar 依赖和 slf4j-jdk14 依赖一起导入项目中。</li>
</ul>
</blockquote>
<p><strong>注意：</strong> 所有的桥接都只对 Logger 对象有效，如果程序中调用了内部的配置类或者是 Appender、Filter 等对象，将无法产生效果。</p>
<h2 id="Logback-日志框架"><a href="#Logback-日志框架" class="headerlink" title="Logback 日志框架"></a>Logback 日志框架</h2><h3 id="日志组件-2"><a href="#日志组件-2" class="headerlink" title="日志组件"></a>日志组件</h3><ul>
<li><strong>Loggers：</strong>应用程序通过 Logger 对象来输出日志信息。</li>
<li><strong>Appenders：</strong> 每个 Logger 对象可以关联多个 Appenders 对象，Logger 对象会将日志信息交给 Appenders 对象处理，由 Appenders 对象决定日志的输出位置。</li>
<li><strong>Layout：</strong>每个 Appenders 对象都会关联一个 Layout 对象，由 Layout  对象对日志信息格式化。</li>
</ul>
<h3 id="配置文件-1"><a href="#配置文件-1" class="headerlink" title="配置文件"></a>配置文件</h3><p>在 resources 目录中创建 logback.xml 配置文件。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 日志输出格式 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;pattern&quot;</span> <span class="attr">value</span>=<span class="string">&quot;%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%thread] [%-5level] %c:%L   %msg%n&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 绝对路径 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;absolutely_log_dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;D:\\Temp&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 相对路径，相对于项目根目录的路径 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;relative_log_dir&quot;</span> <span class="attr">value</span>=<span class="string">&quot;logs\\test&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 配置 ConsoleAppender 属性 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">&quot;console&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.ConsoleAppender&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置日志输出格式 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.classic.encoder.PatternLayoutEncoder&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>$&#123;pattern&#125;<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 修改输出流对象。默认为 System.out --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">target</span>&gt;</span>System.err<span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 配置 FileAppender 属性 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">&quot;file&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.FileAppender&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置日志输出格式 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.classic.encoder.PatternLayoutEncoder&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>$&#123;pattern&#125;<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 日志文件存储路径 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">file</span>&gt;</span>$&#123;absolutely_log_dir&#125;/logback.log<span class="tag">&lt;/<span class="name">file</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 配置 RollingFileAppender 属性 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">&quot;rollFile&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置日志输出格式 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.classic.encoder.PatternLayoutEncoder&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>$&#123;pattern&#125;<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 配置日志文件名生成规则 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">rollingPolicy</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy&quot;</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 通过指定压缩文件名称，来确定分割文件方式 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">fileNamePattern</span>&gt;</span>$&#123;absolutely_log_dir&#125;/rolling.%d&#123;yyyy-MM-dd&#125;_%i.log<span class="tag">&lt;/<span class="name">fileNamePattern</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 文件拆分大小 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">maxFileSize</span>&gt;</span>10KB<span class="tag">&lt;/<span class="name">maxFileSize</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">rollingPolicy</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 配置 filter--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.classic.filter.LevelFilter&quot;</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- 设置拦截日志级别 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">level</span>&gt;</span>error<span class="tag">&lt;/<span class="name">level</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- onMatch 表示匹配上的日志数据，ACCEPT 表示放行 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">onMatch</span>&gt;</span>ACCEPT<span class="tag">&lt;/<span class="name">onMatch</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!-- onMismatch 表示没有匹配上的日志数据，DENY 表示过滤 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">onMismatch</span>&gt;</span>DENY<span class="tag">&lt;/<span class="name">onMismatch</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">filter</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 异步日志 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">&quot;rollFile_async&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.classic.AsyncAppender&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;rollFile&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 配置 rootLogger --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">&quot;ALL&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;console&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;file&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;rollFile&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;rollFile_async&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">root</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--配置自定义 logger。additivity 表示是否从 rootLogger 继承配置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">logger</span> <span class="attr">name</span>=<span class="string">&quot;blog.lin&quot;</span> <span class="attr">level</span>=<span class="string">&quot;debug&quot;</span> <span class="attr">additivity</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;console&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">logger</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p><strong>日志输出格式：</strong></p>
<p>%d{pattern}：操作系统日期。</p>
<p>%thread：线程名称。</p>
<p>%-5level：日志级别。</p>
<p>%c：全限定类名。</p>
<p>%M：方法名称。</p>
<p>%L：行号。</p>
<p>%msg：日志信息。</p>
<p>%n：换行符。</p>
</blockquote>
<h3 id="使用示例-3"><a href="#使用示例-3" class="headerlink" title="使用示例"></a>使用示例</h3><p>导入依赖包：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.7.25<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>ch.qos.logback<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>logback-classic<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在 resources 目录下编写 logback.xml 配置文件：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 日志输出格式 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;pattern&quot;</span> <span class="attr">value</span>=<span class="string">&quot;%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%thread] [%-5level] %c:%L   %msg%n&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 配置 ConsoleAppender 属性 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">&quot;console&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.core.ConsoleAppender&quot;</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置日志输出格式 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span> <span class="attr">class</span>=<span class="string">&quot;ch.qos.logback.classic.encoder.PatternLayoutEncoder&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>$&#123;pattern&#125;<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 修改输出流对象。默认为 System.out --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">target</span>&gt;</span>System.err<span class="tag">&lt;/<span class="name">target</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 配置 rootLogger --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">root</span> <span class="attr">level</span>=<span class="string">&quot;ALL&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">&quot;console&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">root</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>测试 Logback 代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">LogbackTest</span> &#123;</span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testLogback</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="type">Logger</span> <span class="variable">logger</span> <span class="operator">=</span> LoggerFactory.getLogger(LogbackTest.class);</span><br><span class="line">        logger.error(<span class="string">&quot;error&quot;</span>);</span><br><span class="line">        logger.warn(<span class="string">&quot;warn&quot;</span>);</span><br><span class="line">        logger.info(<span class="string">&quot;info&quot;</span>);</span><br><span class="line">        logger.debug(<span class="string">&quot;debug&quot;</span>);</span><br><span class="line">        logger.trace(<span class="string">&quot;trace&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</div>
        </div>
        
        <footer class="kratos-entry-footer clearfix">
            
            <div class="footer-tag clearfix">
                <div class="pull-left">
                <i class="fa fa-tags"></i>
                    <a class="tag-none-link" href="/blog/tags/Java/" rel="tag">Java</a>
                </div>
                <div class="pull-date">
                    <time datetime="2025-11-20T11:35:22.197Z" itemprop="dateModified">最后编辑：2025-11-20</time>
                </div>
            </div>
        </footer>
    </div>
    
        <nav class="navigation post-navigation clearfix" role="navigation">
            
            <div class="nav-previous clearfix">
                <a title=" 【ES】搜索语法" href="/blog/2024/04/06/SpringCloud/ElasticStack/Elasticsearch搜索语法/">&lt; 上一篇</a>
            </div>
            
            
        </nav>
    
    
</article>

        

            </section>

        

                
            

<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
    <!-- 文章和页面根据splitter来分割，没有的话就从头开始设置为sticky -->
    
    
                <aside id="krw-about" class="widget widget-kratos-about clearfix">
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="/blog/images/avatar.webp" loading="lazy" decoding="auto" />
        </div>
    </div>
    <div class="textwidget">
        <p class="text-center">极简主义</p>
    </div>
    <div class="site-meta">
        <a class="meta-item" href="/blog/archives/">
            <span class="title">
                文章
            </span>
            <span class="count">
                72
            </span>
        </a>
        <a class="meta-item" href="/blog/categories/">
            <span class="title">
                分类
            </span>
            <span class="count">
                9
            </span>
        </a>
        <a class="meta-item" href="/blog/tags/">
            <span class="title">
                标签
            </span>
            <span class="count">
                27
            </span>
        </a>
    </div>
</aside>
            
                    <div class="sticky-area">
                
                    <aside id="krw-toc" class="widget widget-kratos-toc clearfix toc-div-class" >
    <div class="photo-background"></div>
    <h4 class="widget-title no-after">
        <i class="fa fa-compass"></i>
        文章目录
        <span class="toc-progress-bar" role="progressbar" aria-label="阅读进度："></span>
    </h4>
    <div class="textwidget">
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#JAVA-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-text">JAVA 日志框架</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JUL-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-text">JUL 日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D"><span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E5%BF%97%E7%BB%84%E4%BB%B6"><span class="toc-text">日志组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B"><span class="toc-text">使用示例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E6%97%A5%E5%BF%97%E9%85%8D%E7%BD%AE"><span class="toc-text">修改日志配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E4%BB%A3%E7%A0%81%E4%BF%AE%E6%94%B9"><span class="toc-text">通过代码修改</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BF%AE%E6%94%B9"><span class="toc-text">通过配置文件修改</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Log4j-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-text">Log4j 日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E5%BF%97%E7%BB%84%E4%BB%B6-1"><span class="toc-text">日志组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-text">配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-1"><span class="toc-text">使用示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JCL-%E6%97%A5%E5%BF%97%E6%8E%A5%E5%8F%A3"><span class="toc-text">JCL 日志接口</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D-1"><span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-2"><span class="toc-text">使用示例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#SLF4J-%E6%97%A5%E5%BF%97%E6%8E%A5%E5%8F%A3"><span class="toc-text">SLF4J 日志接口</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D-2"><span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SLF4J-%E6%95%B4%E5%90%88%E5%85%B6%E5%AE%83%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-text">SLF4J 整合其它日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Log4j2"><span class="toc-text">SLF4J + Log4j2</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Logback"><span class="toc-text">SLF4J + Logback</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-NOP"><span class="toc-text">SLF4J + NOP</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-JCL"><span class="toc-text">SLF4J + JCL</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Log4j"><span class="toc-text">SLF4J + Log4j</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-JUL"><span class="toc-text">SLF4J + JUL</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#SLF4J-Slf4j-simple"><span class="toc-text">SLF4J + Slf4j-simple</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A1%A5%E6%8E%A5%E6%97%A7%E7%9A%84%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-text">桥接旧的日志框架</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Logback-%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6"><span class="toc-text">Logback 日志框架</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%97%A5%E5%BF%97%E7%BB%84%E4%BB%B6-2"><span class="toc-text">日志组件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-1"><span class="toc-text">配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E7%A4%BA%E4%BE%8B-3"><span class="toc-text">使用示例</span></a></li></ol></li></ol>
    </div>
</aside>
                
                
  <aside id="krw-categories" class="widget widget-kratos-categories clearfix">
    <h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Docker/">Docker</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Idea/">Idea</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Java/">Java</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Linux/">Linux</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/MySQL/">MySQL</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Python/">Python</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Selenium/">Selenium</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/SpringBoot/">SpringBoot</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/SpringCloud/">SpringCloud</a><span class="category-list-count">13</span></li></ul>
  </aside>


            
                
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/blog/tags/CORS/" style="font-size: 0.8em;">CORS</a> <a href="/blog/tags/Docker/" style="font-size: 0.8em;">Docker</a> <a href="/blog/tags/Dubbo/" style="font-size: 0.8em;">Dubbo</a> <a href="/blog/tags/ELK/" style="font-size: 0.8em;">ELK</a> <a href="/blog/tags/Elasticsearch/" style="font-size: 0.8em;">Elasticsearch</a> <a href="/blog/tags/Eureka/" style="font-size: 0.8em;">Eureka</a> <a href="/blog/tags/Feign/" style="font-size: 0.8em;">Feign</a> <a href="/blog/tags/Gateway/" style="font-size: 0.8em;">Gateway</a> <a href="/blog/tags/Idea/" style="font-size: 0.8em;">Idea</a> <a href="/blog/tags/Java/" style="font-size: 0.8em;">Java</a> <a href="/blog/tags/JavaWeb/" style="font-size: 0.8em;">JavaWeb</a> <a href="/blog/tags/Linux/" style="font-size: 0.8em;">Linux</a> <a href="/blog/tags/Maven/" style="font-size: 0.8em;">Maven</a> <a href="/blog/tags/MySQL/" style="font-size: 0.8em;">MySQL</a> <a href="/blog/tags/Mybatis/" style="font-size: 0.8em;">Mybatis</a> <a href="/blog/tags/Nacos/" style="font-size: 0.8em;">Nacos</a> <a href="/blog/tags/Pytest/" style="font-size: 0.8em;">Pytest</a> <a href="/blog/tags/Python/" style="font-size: 0.8em;">Python</a>
      </div>
  </aside>

            
                
  <aside id="krw-posts" class="widget widget-kratos-posts">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/blog/2024/04/07/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/"><i class="fa  fa-book"></i> 【Java】常见的日志框架</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/06/SpringCloud/ElasticStack/Elasticsearch%E6%90%9C%E7%B4%A2%E8%AF%AD%E6%B3%95/"><i class="fa  fa-book"></i> 【ES】搜索语法</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/03/SpringCloud/ElasticStack/Elasticsearch%E7%B4%A2%E5%BC%95%E9%85%8D%E7%BD%AE/"><i class="fa  fa-book"></i> 【ES】索引配置</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/02/SpringCloud/ElasticStack/Kibana%E5%B8%B8%E7%94%A8%E5%8A%9F%E8%83%BD/"><i class="fa  fa-book"></i> 【Kibana】常用功能</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/02/SpringCloud/ElasticStack/IK%E5%88%86%E8%AF%8D%E5%99%A8/"><i class="fa  fa-book"></i> 【IK 分词器】基本使用</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

            
    </div>
</section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer"  >
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        <!-- Keep for compatibility -->
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        <!-- New links -->
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2025 溪岚花的博客 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Site built with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by 林家隆.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://gitee.com/" target="_blank">Gitee Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/blog/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="/blog/vendors/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="/blog/vendors/nprogress@0.2.0/nprogress.js"></script>
<script>
    if (!window.kr) {
        window.kr = {};
    }
    window.kr.notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));
    window.kr.siteRoot = "/blog/";
</script>





    <script defer src="/blog/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="/blog/js/kratosr.min.js"></script>
<script defer src="/blog/js/pjax.min.js"></script>



<!-- Extra support for third-party plguins  -->


    </body>
</html>